#include<iostream>
using namespace std;
const int N = 1e6 + 10;
int p[N];
int n, m, k;
int find(int x)
{
	if (x != p[x])p[x] = find(p[x]);
	return p[x];
}
int main()
{
	cin >> n >> m;
	cin >> k;
	for (int i = 1; i <= n * m; i++)p[i] = i;
	while (k--) {
		int a, b;
		cin >> a >> b;
		int pa = find(a);
		int pb = find(b);
		if (pa != pb) {
			p[pa] = pb;
		}
	}
	int cnt = 0;
	for(int i=1;i<=n*m;i++){
		cnt += (p[i] == i ? 1 : 0);
	}
	cout << cnt << endl;
	return 0;
}